<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <!-- 入库 -->
        <el-form
          :inline="true"
          :model="formputIn"
          ref="formputIn"
          :rules="rules"
          class="demo-form-inline"
        >
          <el-form-item label="运单号" prop="num">
            <el-input
              v-model="formputIn.num"
              placeholder="请填写运单号"
            ></el-input>
          </el-form-item>
          <!-- <el-form-item label="扫码">
            <el-input
              v-model="formputIn.waybillNumber"
              placeholder="请扫码-非必填"
            ></el-input>
          </el-form-item>
          <el-form-item label="手动">
            <el-input
              v-model="formputIn.waybillNumber"
              placeholder="非必填"
            ></el-input>
          </el-form-item> -->
          <el-form-item label="请选择仓库" prop="depId">
            <el-select
              v-model="formputIn.depId"
              placeholder="请选择活动区域"
              @change="changesel"
            >
              <el-option
                v-for="(item, index) in selAllDep"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>

          <!-- <el-form-item label="是否是最大包裹" prop="ifBig">
            <el-radio-group v-model="formputIn.ifBig" @change="changeTheme">
              <el-radio label="1">是</el-radio>
              <el-radio label="0">否</el-radio>
            </el-radio-group>
          </el-form-item> -->

          <el-form-item label="重量" prop="weight">
            <el-input
              v-model="formputIn.weight"
              placeholder="请填写重量"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              class="keyputin"
              @click="putInonSubmit('formputIn')"
              >一键入库</el-button
           
            >
          </el-form-item>
        </el-form>

        <!-- 通过运单号查询订单 -->
        <el-form :inline="true" :model="formInline" class="demo-form-inline" >
          <el-form-item label="运单号" prop="num" >
            <el-input
              v-model="formInline.num"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="国际订单号" prop="userId" >
            <el-input
              v-model="formInline.userId"
              placeholder="请输入"
              
            ></el-input>
            </el-form-item>
            <el-form-item label="客户名称" prop="internationalOrderId">
            <el-input
              v-model="formInline.internationalOrderId"
              placeholder="请输入"
            ></el-input>
              </el-form-item>

            <el-form-item label="区位查询" prop="fpartitionId">
            <el-input
              v-model="formInline.fpartitionId"
              placeholder="请输入"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="orderNumSubmit">查询</el-button>
          </el-form-item>
        </el-form>
      </el-card>
      <el-card>
        <el-tabs>
          <!-- <el-table-column label="序号" type="index" width="80" /> -->
          <el-table row-key="id" :data="list" border>
            <!-- <el-table-column label="" prop="name" >
              <template #default="{ row }">
                {{ row.id }}
              </template> -->
            <!-- </el-table-column> -->
            <el-table-column label="用户id" prop="userId" width="100" />
            <el-table-column label="运单号" prop="num"  width="200"/>
            <el-table-column label="国际订单号" prop="internationalOrderId" width="100" />
            <el-table-column label="物品重量" prop="weight" />
            <el-table-column label="体积重" prop="volumeWeight" />
            <el-table-column label="货物品名" prop="nameOfGoods" />
            <el-table-column label="包裹类型" prop="packageType" />
            <el-table-column label="价值（RMB）" prop="value" width="120"/>
            <el-table-column label="入库时间" prop="intoTime"  width="180"/>
            <el-table-column label="出库时间" prop="outTime"  width="180"/>
            <el-table-column label="业务员" prop="operatorId" />
            <el-table-column label="支付方式" prop="paymentType" />
            <el-table-column label="付款金额" prop="paymentAmount" />
            <el-table-column label="仓库名" prop="depName" />
            <el-table-column label="库位号" prop="finalLocation" width="90" />
    
            <el-table-column label="订单状态" prop="status" />
            <el-table-column prop="text" fixed="right" label="操作" width="150">
              <template slot-scope="scope">
              
                <el-button
                  size="small"
                  type="primary"
                  @click="handleEdit(scope.row)"
                  >编辑</el-button
                >
                <el-button
                  size="small"
                  type="danger"
                  @click="handleDelete(scope.row.id)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tabs>
      </el-card>
      <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
        <span>确定删除吗？</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="deleteComfirm">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 弹框 -->
      <el-dialog :title="showTitle" :visible="showDialog" @close="closeDialog">
        <el-form v-loading="loading" label-width="130px">
          <el-form-item label="用户id" prop="userId">
            <el-input v-model="formData.userId" placeholder="请输入" />
          </el-form-item>

          <el-form-item label="运单号" prop="num">
            <el-input v-model="formData.num" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="国际订单id" prop="internationalOrderId">
            <el-input
              v-model="formData.internationalOrderId"
              placeholder="请输入"
            />
          </el-form-item>

          <el-form-item label="物品重量" prop="weight">
            <el-input v-model="formData.weight" placeholder="请输入" />
          </el-form-item>

          <el-form-item label="体积重" prop="volumeWeight">
            <el-input v-model="formData.volumeWeight" placeholder="请输入" />
          </el-form-item>

          <el-form-item label="物品名称" prop="nameOfGoods">
            <el-input v-model="formData.nameOfGoods" placeholder="请输入" />
          </el-form-item>

          <el-form-item label="包裹类型" prop="packageType">
            <el-input v-model="formData.packageType" placeholder="请输入" />
          </el-form-item>

          <el-form-item label="价值（RMB）" prop="value">
            <el-input v-model="formData.value" placeholder="请输入" />
          </el-form-item>

          <el-form-item label="入库时间" prop="intoTime">
          <!-- <el-input v-model="formData.intoTime" placeholder="请输入" /> -->
          <el-date-picker
          v-model="formData.intoTime"
          type="datetime"
          placeholder="请输入">
         </el-date-picker>
          
          </el-form-item>
          <el-form-item label="出库时间" prop="outTime">
           <el-date-picker
          v-model="formData.outTime"
          type="datetime"
          placeholder="请输入">
        </el-date-picker>
          
          </el-form-item>


          <el-form-item label="业务员" prop="operatorId">
            <el-input v-model="formData.operatorId" placeholder="请输入" />
          </el-form-item>

          <el-form-item label="国内订单支付方式" prop="paymentType">
            <el-input v-model="formData.paymentType" placeholder="请输入" />
          </el-form-item>

          <el-form-item label="付款金额" prop="paymentAmount">
            <el-input v-model="formData.paymentAmount" placeholder="请输入" />
          </el-form-item>
<!-- 
          <el-form-item label="仓库名" prop="depName">
            <el-input v-model="formData.depName" placeholder="请输入" />
          </el-form-item> -->

          <!-- <el-form-item label="库位号" prop="finalLocation">
            <el-input v-model="formData.finalLocation" placeholder="请输入" />
          </el-form-item> -->
<!-- 
          <el-form-item label="订单状态" prop="status">
            <el-input v-model="formData.status" placeholder="请输入" />
          </el-form-item> -->
         
         

        </el-form>

        <template #footer>
          <div style="text-align: right">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="clickSubmit">确认</el-button>
          </div>
        </template>
      </el-dialog>
    </div>

    <!-- 分页 -->
    <div align="center">
      <el-pagination
        :current-page.sync="currentPage"
        layout="prev, pager, next, jumper"
        :total="list.length"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import {
  SelectAll2,
  UpdateAll,
  deleteById,
  putStorage,
  SelectByNum,
} from "@/api/country";

import { selectAllDep, } from "@/api/depository";
export default {
  name: "Permission",
  data() {
    

    //只能输入数字
   /*  var validateNum = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入运单号"));
      } else {
        let n = /^0|[^\d]/g;
        if (n.test(value)) {
          callback();
        } else {
          new Error("运单号只能由数字组成");
        }
      }
    }; */

    return {

      // 时间格式
      intoTime:'',
      outTime:'',
      
      rules: {
         num:  [{ required: true, message: "请输入运单号", trigger: "blur" }],
        depId: [{ required: true, message: "请选择仓库地址", trigger: "blur" }],
        ifBig:[{ required: true, message: "请选择:是否是最大包裹", trigger: "blur" }],
        weight:[{ required: true, message: "请输入重量", trigger: "blur" }],
      },
      //通过订单号查询数据
      formInline: {
        orderNum: "",
      },

      //一键入库
      formputIn: {
        num: "", //运单号
        depId: "", //仓库id
        ifBig: "", //是否是大货
        weight: "", //重量
      },

      //查询所有仓库
      selAllDep: [],

      //仓库地址id
      depID: "",

      dialogVisible: false,
      // 分页数据
      currentPage: 1,
      list: [],
      pageParam: {
        // 分页参数对象
        page: "",
        num: "",
      },
      showDialog: false,
      formData: {
        // enVisible: '0', // 0不开启, 1开启 (注意: 不是布尔值, 所以将来需要处理switch)
        username: "", // 名称
        password: "",
      },
      loading: false,
      permissionData: [], // 存放用于展示的权限列表数据 (树形结构)
    };
  },
  computed: {
    showTitle() {
      if (this.formData.id) {
        return "编辑";
      }
    },
  },
  mounted() {
    console.log("进入页面的钩子函数");
    this.getPermissionList();
    //查询所有仓库
    this.getAllDep();
  },

  methods: {
    //通过订单号查询---提交
    async orderNumSubmit() {
      const data = await SelectByNum({
        num: this.formInline.orderNum,
      });

      if (data.length == 0) {
        this.$message.success("暂无此订单数据");
        setTimeout(() => {
          this.getPermissionList();
        }, 2000);
      } else {
        this.list = data;
        this.dataTotal = data.length;
        console.log("通过运单号查询", data);
      }
    },

    //查询所有仓库
    async getAllDep() {
      const data = await selectAllDep({});

      console.log("查询所有仓库", data);
      this.selAllDep = data;
    },

    //查询仓库
    changesel(val) {
      console.log("val", val); //id的值
      this.depID = val;
    },

    //是否是最大包裹
    changeTheme(val) {
      console.log("是否是最大包裹", val);
      this.formputIn.ifBig = val;
    },

    //一键入库---提交
    putInonSubmit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const data = await putStorage({
            num: this.formputIn.num,
            depId: parseInt(this.formputIn.depId),
            ifBig: parseInt(this.formputIn.ifBig),
            weight: parseInt(this.formputIn.weight),
            
          });

          console.log("一键入库", data);
          this.$message.success(data)
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    //查询页面数据
    async getPermissionList() {
      let data = await SelectAll2(this.pageParam);
      console.log("请求响应结果", data);
      //查询
      this.list = data.list;
      this.dataTotal = data.total;
    },
    clickAdd(type, pid) {
      this.showDialog = true;
      // 点击显示弹框时, 立刻记录type, pid
      // this.formData.type = type
      // this.formData.pid = pid
    },
    closeDialog() {
      this.showDialog = false;
      // 重置清空数据
      this.formData = {
        enVisible: "0", // 0不开启, 1开启 (注意: 不是布尔值, 所以将来需要处理switch)
        name: "", // 名称
        code: "", // 权限标识
        description: "", // 描述
        type: "", // 类型, 1一级访问权限, 2二级操作权限
        pid: "", // 添加到哪个节点下, 树形结构渲染的, 点击某个节点时, 也能知道pid是什么
      };
    },
    // 点击弹框的确认按钮
    async clickSubmit() {
      if (this.formData.username === "" || this.formData.password === "") {
        this.$message.error("用户名和密码不能为空");
        return;
      }
      if (this.formData.id) {
        // 编辑操作
        const { postalcode, ...formData } = this.formData;
        await UpdateAll(formData);
      }
      this.showDialog = false;
      this.$message.success("操作成功");
      this.getPermissionList();
    },
    // 删除功能
    handleDelete(id) {
      this.deleteId = id;
      this.dialogVisible = true;
    },
    async deleteComfirm() {
      this.dialogVisible = false;
      await deleteById({
        id: this.deleteId,
      });
      this.$message({
        message: "删除成功",
        type: "success",
      });
      this.getPermissionList();
    },
    handleEdit(row) {
      console.log(row);
      // const {postalCode,...formData} = row
      // formData.postalcode = postalCode
      this.formData = row;
      this.formData.postalcode = row.postalCode;
      this.showDialog = true;
    },
    // 点击查看编辑按钮, 显示弹框
    clickEdit(id) {
      // 显示弹框
      this.showDialog = true;
      this.loading = true;
      // 发送请求, 获取数据, 进行回显
      // const { data } = await reqGetPermissionDetail(id)
      // this.formData = data

      this.loading = false;
    },

    //仓库
    selectDep() {},

    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    },
  },

  created() {},
};
</script>

<style lang="scss" scoped>
//运单号
.el-form-item,
.el-form-item--medium {
  margin-left: 0.6%;
}
.keyputin {
  margin-left: 20px;
}
</style>
